<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      inline
      v-show="showSearch"
      label-width="45px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="手机号" prop="phone" label-width="60px">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 操作功能 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      max-height="650"
      @selection-change="handleSelectionChange"
    >
      <el-table-column align="center" min-width="70" prop="name" label="姓名" />
      <el-table-column align="center" min-width="70" prop="sex" label="性别">
        <template slot-scope="{ row }">
          <el-tag :type="row.sex == '男' ? '' : 'danger'">{{ row.sex }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" min-width="70" prop="age" label="年龄" />
      <el-table-column
        align="center"
        min-width="110"
        prop="phone"
        label="手机号"
      />
      <el-table-column
        align="center"
        min-width="110"
        prop="avatar"
        label="患者头像"
      >
        <template slot-scope="{ row }">
          <image-preview :src="row.avatar" />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        min-width="300"
        prop="remark"
        label="医生简介"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        min-width="200"
        prop="createTime"
        label="医生信息创建时间"
      />
      <el-table-column align="center" width="200" label="操作">
        <template slot-scope="{ row, $index }">
          <el-link
            type="primary"
            icon="el-icon-view"
            :underline="false"
            @click="handleCheck(row)"
            >查看</el-link
          >
          <el-link
            style="margin: 0 10px"
            type="success"
            icon="el-icon-edit"
            :underline="false"
            @click="handleUpdate(row)"
            >修改</el-link
          >
          <el-link
            type="danger"
            icon="el-icon-delete"
            :underline="false"
            @click="handleDel(row, $index)"
            v-if="isAdmin"
            >删除</el-link
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="tableData.length > 0"
      :total="tableData.length"
      :page.sync="queryParams.pageIndex"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 弹窗 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="80%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="ruleForm.name"
            clearable
            placeholder="请输入姓名"
            :disabled="disabled"
          />
        </el-form-item>

        <el-form-item label="性别" prop="sex">
          <el-select
            class="wid"
            v-model="ruleForm.sex"
            clearable
            placeholder="请选择性别"
            :disabled="disabled"
          >
            <el-option
              v-for="item in sexOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="年龄" prop="age">
          <el-input
            v-model="ruleForm.age"
            clearable
            placeholder="请输入年龄"
            :disabled="disabled"
          />
        </el-form-item>

        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="ruleForm.phone"
            clearable
            placeholder="请输入手机号"
            :disabled="disabled"
          />
        </el-form-item>

        <el-form-item label="患者头像" prop="avatar">
          <ImageUpload
            ref="ImageUpload"
            @input="imgInput"
            :disabled="disabled"
          />
        </el-form-item>

        <el-form-item label="密码" prop="password" v-if="title == '新增用户'">
          <el-input
            v-model="ruleForm.password"
            clearable
            placeholder="请输入密码"
            type="password"
            show-password
          />
        </el-form-item>

        <el-form-item label="医生简介" prop="remark">
          <el-input
            v-model="ruleForm.remark"
            placeholder="请输入医生简介"
            type="textarea"
            :rows="6"
            :disabled="disabled"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          v-if="title != '用户信息'"
          type="primary"
          @click="submitForm('ruleForm')"
          :loading="btnLoading"
          >确 定</el-button
        >
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, addUser, updateUser } from "@/api/system/user";
export default {
  data() {
    return {
      isAdmin: localStorage.getItem("role") === "admin",
      // 防多次点击
      btnLoading: false,
      // 是否修改
      disabled: false,
      // 条数
      total: 0,
      // 搜索条件
      queryParams: {
        phone: undefined,
        sex: undefined,
        name: undefined,
        pageIndex: 1,
        pageSize: 10,
      },
      // 性别下拉
      sexOptions: [
        { label: "男", value: "男" },
        { label: "女", value: "女" },
      ],
      // 表格数据
      tableData: [],
      // 是否显示搜索
      showSearch: true,
      // 表单校验
      rules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        sex: [{ required: true, message: "请选择性别", trigger: "blur" }],
        age: [{ required: true, message: "请输入年龄", trigger: "blur" }],
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      // 表单数据
      ruleForm: {},
      // 弹窗状态
      dialogVisible: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹窗顶部文本
      title: "",
    };
  },
  methods: {
    imgInput(e) {
      this.ruleForm.avatar = e;
    },
    // 图片填充
    ImageFilling(row) {
      this.$refs.ImageUpload.fileList = [];
      if (row.avatar)
        this.$refs.ImageUpload.fileList.push({
          url: row.avatar,
          name: row.avatar,
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageIndex = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (!valid) return;
        this.btnLoading = true;
        const actionFn =
          this.title == "新增用户" ? this.addFunc : this.editFunc;
        try {
          const { code, msg } = await actionFn(this.ruleForm);
          if (code === 200) {
            this.$modal.msgSuccess(msg);
            this.dialogVisible = false;
            this.btnLoading = false;
          }
        } catch (err) {
          this.btnLoading = false;
        }
      });
    },
    // 新增
    async addFunc(ruleForm) {
      ruleForm.createTime = this.parseTime(new Date());
      await addUser(ruleForm);
      this.tableData.push(ruleForm);
      return { code: 200, msg: "新增用户成功" };
    },
    // 修改
    async editFunc(ruleForm) {
      await updateUser(ruleForm);
      await this.getList();
      return { code: 200, msg: "用户数据修改成功" };
    },
    // 多选
    handleSelectionChange(value) {
      this.selectList = JSON.parse(JSON.stringify(value));
      this.ids = value.map((item) => item.id);
      this.single = value.length !== 1;
      this.multiple = !value.length;
    },
    // 删除
    handleDel(row, index) {
      const ids = row.fid || this.ids;
      this.$modal
        .confirm(`是否确认删除此数据`)
        .then(() => {})
        .then(() => {
          this.getList();
          this.tableData.splice(index, 1);
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 修改
    handleUpdate(row) {
      this.title = "修改用户信息";
      this.ruleForm = row;
      this.disabled = false;
      this.dialogVisible = true;
      setTimeout(() => {
        this.ImageFilling(this.ruleForm);
      }, 500);
    },
    // 新增
    handleAdd() {
      this.ruleForm = {
        name: undefined,
        sex: undefined,
        age: undefined,
        phone: undefined,
        password: "123456",
      };
      this.title = "新增用户";
      this.disabled = false;
      this.dialogVisible = true;
    },
    // 查看
    handleCheck(row) {
      this.title = "用户信息";
      this.disabled = true;
      this.ruleForm = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
      setTimeout(() => {
        this.ImageFilling(this.ruleForm);
      }, 500);
    },
    // 获取数据
    async getList() {
      let data = await listUser(this.queryParams);
      this.tableData = data.data.list;
    },
  },
  created() {
    this.getList();
  },
};
</script>

<style lang="scss" scoped>
.wid {
  width: 100%;
}
</style>
